import pandas as pd
import time

def save_data(raw_data, predicted_label):
    """保存数据和预测结果"""
    if not raw_data:
        return

    timestamp = time.strftime("%Y%m%d_%H%M%S")
    filename = f'./csvdata/har_data_{timestamp}.csv'

    # 提取各轴数据
    acc_x = [sample[0] for sample in raw_data]
    acc_y = [sample[1] for sample in raw_data]
    acc_z = [sample[2] for sample in raw_data]
    gyro_x = [sample[3] for sample in raw_data]
    gyro_y = [sample[4] for sample in raw_data]
    gyro_z = [sample[5] for sample in raw_data]

    # 创建DataFrame
    df = pd.DataFrame({
        'acc_x': acc_x,
        'acc_y': acc_y,
        'acc_z': acc_z,
        'gyro_x': gyro_x,
        'gyro_y': gyro_y,
        'gyro_z': gyro_z,
        'predicted_label': [predicted_label] * len(acc_x)
    })

    # 保存为CSV文件
    df.to_csv(filename, index=False)
    print(f"数据已保存为 {filename}")